from link_list.node import SinglyListNode


def splitList(first1: SinglyListNode, pre, post) -> SinglyListNode:
    result1 = [first1]
    i = 2
    while i < pre:
        result1[0] = result1[0].next
        i += 1
    result1.append(result1[0].next)
    while i < post:
        result1[1] = result1[1].next
        i += 1

    if result1[1] is not None:
        result1.append(result1[1].next)
        result1[1].next = None

    reverseList(result1[0], result1[0].next, result1[2])
    return first1


def reverseList(head: SinglyListNode, curr: SinglyListNode, behind: SinglyListNode):
    post = None
    pre = behind
    while curr is not None:
        post = curr.next
        curr.next = pre
        pre = curr
        curr = post
    head.next = pre


first = SinglyListNode(1)
first.next = SinglyListNode(2)
first.next.next = SinglyListNode(3)
first.next.next.next = SinglyListNode(4)
first.next.next.next.next = SinglyListNode(5)
result = splitList(first, 2, 4)
while result is not None:
    # 原顺序3215879
    print(result.val, end="")
    result = result.next
